package com.encyclopedia.service.impl;

import com.aliyuncs.utils.StringUtils;
import com.encyclopedia.controller.AdminPlantController;
import com.encyclopedia.domain.Plant;
import com.encyclopedia.domain.PlantBreed;
import com.encyclopedia.domain.PlantEcotope;
import com.encyclopedia.domain.PlantGrowthCharacteristic;
import com.encyclopedia.domain.vo.PlantInfoVo;
import com.encyclopedia.mapper.AdminPlantMapper;
import com.encyclopedia.mapper.PlantBreedMapper;
import com.encyclopedia.mapper.PlantEcotopeMapper;
import com.encyclopedia.mapper.PlantGrowthCharacteristicMapper;
import com.encyclopedia.result.JsonRequest;
import com.encyclopedia.service.ArticleService;
import com.encyclopedia.service.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author MaLiang
 * @version 1.0
 * @date 2024-05-04 19:54
 */
@Service
@Slf4j
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private AdminPlantMapper adminPlantMapper;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private PlantBreedMapper plantBreedMapper;

    @Autowired
    private PlantEcotopeMapper plantEcotopeMapper;

    @Autowired
    private PlantGrowthCharacteristicMapper plantGrowthCharacteristicMapper;


    /**
     * 查看修改文章列表
     *
     * @return
     */
    @Override
    public List<Plant> getInfoArticle() {
        List<Integer> plantId = adminPlantMapper.selectPlntId();
        PlantInfoVo plantInfoVo = new PlantInfoVo();
        List<PlantInfoVo> plantInfoVoList = new ArrayList<>();
        if (plantId.size() > 0) {
            for (Integer id : plantId) {
                plantInfoVo = (PlantInfoVo) redisUtils.get("plantplantListUpdate:" + id);
                if (!ObjectUtils.isEmpty(plantInfoVo)) {
                    plantInfoVoList.add(plantInfoVo);
                }
            }
        }
        List<Plant> plantList = new ArrayList<>();
        for (PlantInfoVo infoVo : plantInfoVoList) {
            plantList.add(infoVo.getPlant());
        }
        log.info("=========================获取存入的数据,{}", plantList);
//        Plant plant = plantInfoVo.getPlant();
        return plantList;
    }

    /**
     * 查看修改文章详情
     *
     * @param id
     * @return
     */
    @Override
    public PlantInfoVo getArticleById(Integer id) {
        PlantInfoVo plantInfoVo = (PlantInfoVo) redisUtils.get("plantplantListUpdate:" + id);
        return plantInfoVo;
    }

    @Override
    public int updateArticleById(PlantInfoVo plantInfoVo) {
        Plant plant = new Plant();

        if (plantInfoVo.getPlantBreed().getBreedId() != null) {
            plantBreedMapper.updatePlantBreed(plantInfoVo.getPlantBreed());
        } else {
            PlantBreed plantBreed = plantInfoVo.getPlantBreed();
            plantBreed.setPlantId(plantInfoVo.getPlant().getId());
            plantBreedMapper.insertPlantBreed(plantBreed);
            plant.setBreedId(plantInfoVo.getPlantBreed().getBreedId());
        }
        if (plantInfoVo.getPlantEcotope().getEcotopeId() != null) {
            plantEcotopeMapper.updatePlantEcotope(plantInfoVo.getPlantEcotope());
        } else {
            PlantEcotope plantEcotope = plantInfoVo.getPlantEcotope();
            plantEcotope.setPlantId(plantInfoVo.getPlant().getId());
            plantEcotopeMapper.insertPlantEcotope(plantEcotope);
            plant.setEctopeId(plantInfoVo.getPlantEcotope().getEcotopeId());
        }
        if (plantInfoVo.getPlantGrowthCharacteristic().getGrowthCharacteristicId() != null) {
            plantGrowthCharacteristicMapper.updatePlantGrowthCharacteristic(plantInfoVo.getPlantGrowthCharacteristic());
        } else {
            PlantGrowthCharacteristic plantGrowthCharacteristic = plantInfoVo.getPlantGrowthCharacteristic();
            plantGrowthCharacteristic.setPlantId(plantInfoVo.getPlant().getId());
            plantGrowthCharacteristicMapper.insertPlantGrowthCharacteristic(plantInfoVo.getPlantGrowthCharacteristic());
            plant.setGrowthCharacteristicId(plantInfoVo.getPlantGrowthCharacteristic().getGrowthCharacteristicId());
        }

        plant.setId(plantInfoVo.getPlant().getId());
        plant.setCommonName(plantInfoVo.getPlant().getCommonName());
        plant.setScientificName(plantInfoVo.getPlant().getScientificName());
        plant.setDescription(plantInfoVo.getPlant().getDescription());
        plant.setDateAdded(plantInfoVo.getPlant().getDateAdded());
        plant.setLastUpdated(plantInfoVo.getPlant().getLastUpdated());

        redisUtils.del("plantplantListUpdate:" + plantInfoVo.getPlant().getId());

        return adminPlantMapper.updatePlant(plant);
    }

    @Override
    public JsonRequest deleteArticleById(Integer ids) {
        redisUtils.del("plantplantListUpdate:"+ids);
        return JsonRequest.success("200");
    }
}
